<?php
namespace app\applet\controller;

use app\applet\BaseController;

class ChartController extends BaseController
{
    public function indexAction()
    {
        try {
            //1. 一楼广告位
            $broadcasts = $this->app->broadcastService->getBroadcastByPositionId(1, $this->appletId);

            $data['first_floor'] = $broadcasts;

            //2。二楼广告位
            $broadcasts = $this->app->broadcastService->getBroadcastByPositionId(2, $this->appletId);

            $data['second_floor'] = $broadcasts;

            //3。排行榜
            $charts = $this->app->chartService->getCharts($this->appletId);

            $data['chart'] = $charts;

            return json()->data(['code' => 200, 'msg' => '查询成功','data' => $data]);
        }catch(\Exception $e){
            return json()->data(['code'=>$e->getCode(),"msg"=>$e->getMessage()]);
        }
    }

    public function listsAction()
    {
        try {
            $params['where'][] = ['status', '=', 1];
            if (isset($this->get['provinceCode'])) {
                $params['where'][] = ['provinceCode', '=', $this->get['provinceCode']];
            }

            if (isset($this->get['categoryId'])) {
                $params['where'][] = ['categoryId', '=', $this->get['categoryId']];
            }

            $params['order'] = ['creditCount' => 'desc', 'organizationId' => 'desc'];

            $lists = $this->app->organizationService->getLists($params, $this->page, $this->pagesize);

            $data['lists'] = [];

            if (count($lists)) {
                foreach ($lists as $val) {
                    $data['lists'][] = [
                        'organizationId' => $val->organizationId,
                        'organizationName' => $val->organizationName,
                        'organizationLogo' => $val->organizationLogo,
                        'creditCountView' => $val->creditCountView,
                        'personCountView' => $val->personCountView,
                    ];
                }
            }

            $data['totalInfo'] = $this->app->utilService->getMeta($lists);

            return json()->data(['code' => 200, 'msg' => '查询成功','data' => $data]);
        }catch(\Exception $e){
            return json()->data(['code'=>$e->getCode(),"msg"=>$e->getMessage()]);
        }
    }
}
